<template>
  <div class="mod-menu">
    <el-form :inline="true" :model="dataForm">
      <el-form-item>
        <el-button
          type="primary"
          v-if="isAuth('sysMenu:add')"
          @click="addHandle()"
          >新增</el-button
        >
      </el-form-item>
      <el-form-item>
        <el-button
          type="text"
          v-if="isAuth('sysMenu:refence')"
          @click="refenceCache()"
          >刷新缓存</el-button
        >
      </el-form-item>
    </el-form>
    <el-tree
      :data="dataList"
      :props="defaultProps"
      :expand-on-click-node="false"
    >
      <span class="custom-tree-node" slot-scope="{ node, data }">
        <span :class="data.status === 1 ? '' : 'isEnableSpan'">{{
          node.label
        }}</span>
        <span>
          <el-button
            type="text"
            v-if="data.type !== 2 && isAuth('sysMenu:add')"
            size="mini"
            @click="addHandle(data.id)"
          >
            新增
          </el-button>
          <el-button
            type="text"
            v-if="isAuth('sysMenu:edit')"
            size="mini"
            @click="updateHandle(data.id)"
          >
            编辑
          </el-button>
          <el-button
            type="text"
            v-if="isAuth('sysMenu:delete')"
            size="mini"
            @click="deleteHandle(data)"
          >
            删除
          </el-button>
        </span>
      </span>
    </el-tree>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    ></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from "./menu-add-or-update";
import { treeDataTranslate, isAuth } from "@/utils";
import {
  GetMenuListApi,
  GetMenusApi,
  RefenceMenuCacheApi,
  DeleteMenuApi
} from "@/api/menu";
export default {
  data() {
    return {
      dataForm: {},
      dataList: [],
      dataListLoading: false,
      addOrUpdateVisible: false,
      defaultProps: {
        children: "childrens",
        label: "name"
      }
    };
  },
  components: {
    AddOrUpdate
  },
  activated() {
    this.getDataList();
  },
  methods: {
    // 刷新缓存
    refenceCache() {
      RefenceMenuCacheApi()
        .then(res => {
          window.console.log(res);
        })
        .catch(err => {
          window.console.log(err);
        });
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      GetMenuListApi({ IsDeleted: false })
        .then(res => {
          this.dataList = treeDataTranslate(res.data.data);
          this.dataListLoading = false;
        })
        .catch(err => {
          window.console.log(err);
          this.dataListLoading = false;
        });
    },
    // 新增 / 修改
    addHandle(id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id, true, this.dataList);
      });
    },
    updateHandle(id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id, false, this.dataList);
      });
    },
    // 删除
    deleteHandle(data) {
      var msg = "";
      switch (data.type) {
        case 0:
          msg = `此操作会删除目录下所有菜单和按钮，确定对目录[${
            data.name
          }]进行[删除]操作?`;
          break;
        case 1:
          msg = `此操作会删除菜单下所有按钮，确定对菜单[${
            data.name
          }]进行[删除]操作?`;
          break;
        case 2:
          msg = `确定对按钮[${data.name}]进行[删除]操作?`;
          break;
      }
      this.$confirm(msg, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          DeleteMenuApi(data.id)
            .then(res => {
              window.console.log(res);
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getDataList();
                }
              });
            })
            .catch(err => {
              this.$message.error(data.msg);
            });
          if (false) {
            this.$http({
              url: this.$http.adornUrl(`/sys/menu/delete/${id}`),
              method: "post",
              data: this.$http.adornData()
            }).then(({ data }) => {
              if (data && data.code === 0) {
                this.$message({
                  message: "操作成功",
                  type: "success",
                  duration: 1500,
                  onClose: () => {
                    this.getDataList();
                  }
                });
              } else {
                this.$message.error(data.msg);
              }
            });
          }
        })
        .catch(() => {});
    }
  }
};
</script>
<style scoped>
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
}
.isEnableSpan {
  color: #d6d6d6;
}
</style>
